'use strict'
const path = require('path')

// 设置别名
function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    open: true,
    port: 8080,
    // proxy: {
    //   '/backstage': {
    //     target: 'http://192.168.110.221:9301',
    //     // pathRewrite: { '^/api': '' },
    //     changeOrigin: true,
    //     secure: false
    //   }
    // },
    hotOnly: true
  },
  configureWebpack: () => {
    return {
      name: '好物特工后台管理系统',
      resolve: {
        alias: {
          '@': resolve('src'),
          '@mixins': resolve('src/mixins'),
          '@components': resolve('src/views/components'),
          vendor: resolve('src/vendor')
        }
      }
    }
  },
  chainWebpack: config => {
    // 全局使用scss
    const oneOfsMap = config.module.rule('scss').oneOfs.store
    oneOfsMap.forEach(item => {
      item
        .use('sass-resources-loader')
        .loader('sass-resources-loader')
        .options({
          resources: ['./src/styles/mixin.scss', './src/styles/variables.scss']
        })
        .end()
    })

    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])

    config.plugin('html').tap(args => {
      args[0].title = '好物特工后台管理系统'
      return args
    })

    config.resolve.symlinks(true) // 热更新

    config.plugins.delete('prefetch').delete('preload') // 删除懒加载模块的prefetch preload

    // set svg-sprite-loader
    config.module.rule('svg').exclude.add(resolve('src/icons')).end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config.when(process.env.NODE_ENV !== 'development', config => {
      // 分模块打包
      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [
          {
            inline: /runtime\..*\.js$/
          }
        ])
        .end()
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial'
          },
          elementUI: {
            name: 'chunk-elementUI',
            priority: 20,
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/
          },
          echarts: {
            name: 'chunk-echarts',
            test: /[\\/]node_modules[\\/](vue-)?echarts[\\/]/,
            chunks: 'all',
            priority: 4,
            reuseExistingChunk: true,
            enforce: true
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'),
            minChunks: 3,
            priority: 5,
            reuseExistingChunk: true
          },
          styles: {
            name: 'chunk-styles',
            test: /\.(sa|sc|c)ss$/,
            chunks: 'all',
            enforce: true
          }
        }
      })
      config.optimization.runtimeChunk('single')
    })
  },
  css: {
    sourceMap: false,
    loaderOptions: {
      sass: {
        prependData: `
        @import "@/styles/mixin.scss";
        @import "@/styles/variables.scss";
        `
      }
    }
  }
}
